<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace builder\widget\ajax;

use builder\helper\Url;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;

/**
 * AJAX: dropdown
 * ```php
 * Output:String
 *    WidgetFacade::echo2Return(function () {
 *          echo ButtonDropdown::widget([
 *                'label' => 'Action',
 *                'dropdown' => [
 *                      'items' => [
 *                          ['label' => 'DropdownA', 'url' => '/'],
 *                          ['label' => 'DropdownB', 'url' => '#'],
 *                      ],
 *                 ],
 *              ]);
 *
 *   });
 * ```
 * @author cleverstone
 * @since ym2.0
 */
class ButtonDropdown extends \yii\bootstrap4\ButtonDropdown
{
    /**
     * @var string name of a class to use for rendering dropdowns withing this widget. Defaults to [[Dropdown]].
     */
    public $dropdownClass = 'builder\widget\ajax\DropDown';

    /**
     * {@inheritdoc}
     */
    public function init()
    {
        parent::init();

        $this->buttonOptions['ym2-bootstrap-plugin'] = 'dropdown';
        $this->buttonOptions['plugin-config'] = '';

        unset($this->options['id']);
        unset($this->buttonOptions['id']);
    }

    /**
     * {@inheritdoc}
     * @throws \Throwable
     */
    public function run()
    {
        $html = $this->renderButton() . "\n" . $this->renderDropdown();

        if ($this->renderContainer) {
            Html::addCssClass($this->options, ['widget' => 'drop' . $this->direction, 'btn-group']);
            $options = $this->options;
            $tag = ArrayHelper::remove($options, 'tag', 'div');
            $html = Html::tag($tag, $html, $options);
        }

        return $html;
    }

    /**
     * Generates the button dropdown.
     * @return string the rendering result.
     * @throws \Throwable
     */
    protected function renderButton()
    {
        Html::addCssClass($this->buttonOptions, ['widget' => 'btn']);
        $label = $this->label;
        if ($this->encodeLabel) {
            $label = Html::encode($label);
        }

        if ($this->split) {
            $buttonOptions = $this->buttonOptions;
            $this->buttonOptions['data-toggle'] = 'dropdown';
            $this->buttonOptions['aria-haspopup'] = 'true';
            $this->buttonOptions['aria-expanded'] = 'false';
            Html::addCssClass($this->buttonOptions, ['toggle' => 'dropdown-toggle dropdown-toggle-split']);
            unset($buttonOptions['id']);
            $splitButton = Button::widget([
                'label' => '<span class="sr-only">Toggle Dropdown</span>',
                'encodeLabel' => false,
                'options' => $this->buttonOptions,
                'view' => $this->getView(),
            ]);
        } else {
            $buttonOptions = $this->buttonOptions;
            Html::addCssClass($buttonOptions, ['toggle' => 'dropdown-toggle']);
            $buttonOptions['data-toggle'] = 'dropdown';
            $buttonOptions['aria-haspopup'] = 'true';
            $buttonOptions['aria-expanded'] = 'false';
            $splitButton = '';
        }

        if (isset($buttonOptions['href'])) {
            if (is_array($buttonOptions['href'])) {
                $buttonOptions['href'] = Url::to($buttonOptions['href']);
            }
        } else {
            if ($this->tagName === 'a') {
                $buttonOptions['href'] = '#';
                $buttonOptions['role'] = 'button';
            }
        }

        return Button::widget([
                'tagName' => $this->tagName,
                'label' => $label,
                'options' => $buttonOptions,
                'encodeLabel' => false,
                'view' => $this->getView(),
            ]) . "\n" . $splitButton;
    }
}